package cn.cena._04dirct;

import cn.cena.util.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer2 {



    //  队列名称
    public static final String NAME_QUEUE_DIRCT2="NAME_QUEUE_DIRCT2";

    public static void main(String[] args) throws Exception {

        //  获取连接
        Connection connection = ConnectionUtil.getConnection();

        // 获取与交换机的通道
        final Channel channel = connection.createChannel();

        // 创建队列
        channel.queueDeclare(NAME_QUEUE_DIRCT2, true, false,  false, null);
        // 绑定队列
        channel.queueBind(NAME_QUEUE_DIRCT2, Producer.NAME_EXCHANG_DIRCT, "info");

        // 消费消息的方式
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("拿到消息：" + new String(body));
                System.out.println("consumerTag:"+ consumerTag);
                System.out.println("交换机：" + envelope.getExchange());
                System.out.println("消息Id："+envelope.getDeliveryTag());
                System.out.println("队列："+ envelope.getRoutingKey());


                // 手动进行消息确认
                channel.basicAck(envelope.getDeliveryTag(), false);

            }
        };

        // 监听队列
        channel.basicConsume(NAME_QUEUE_DIRCT2, consumer);
    }
}
